package com.bytezero.login_test.home;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bytezero.login_test.BaseActivity.BaseFragment;
import com.bytezero.login_test.R;
import com.bytezero.login_test.bean.Goods;
import com.bytezero.login_test.home.adapter.HomeRecycleViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Bytezero_zhenglei!
 * @package com.bytezero.login_test.home
 * @fileName
 * @data 2022/1/26 18:18
 * @description
 * @TODO
 * @email 420498246@qq.com
 */
public class HomeFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, HomeContract.IHomeView {


    private HomeRecycleViewAdapter homeRecycleViewAdapter;
    private HomePresenter homePresenter;
    private HomeSpanSizeLookup homeSpanSizeLookup;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        SwipeRefreshLayout swipeRefreshLayout = find(R.id.home_swiperefresh);
        swipeRefreshLayout.setOnRefreshListener(this);


        RecyclerView recyclerView = find(R.id.home_recyclerview);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 4);

        List<Goods> goods = new ArrayList<>();
        homeSpanSizeLookup = new HomeSpanSizeLookup(goods);
        gridLayoutManager.setSpanSizeLookup(homeSpanSizeLookup);
        recyclerView.setLayoutManager(gridLayoutManager);
        homeRecycleViewAdapter = new HomeRecycleViewAdapter(getActivity(), goods);
        recyclerView.setAdapter(homeRecycleViewAdapter);

        homePresenter = new HomePresenter(this);
        homePresenter.getData();

    }

    @Override
    public void onRefresh() {
        homePresenter.getData();
    }

    @Override
    public void getGoodsSucess(List<Goods> goods) {
        homeSpanSizeLookup.setGoods(goods);
        homeRecycleViewAdapter.setGoods(goods);

    }

    @Override
    public void getGoodsError(Throwable throwable) {

    }
}
